package com.escom.service;

import android.support.v4.app.FragmentActivity;
import android.util.Log;

import com.escom.healthlc.entidad.TemperaturaORM;
import com.escom.utils.Constantes;
import com.escom.utils.CustomTemperaturaAdapter;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.StringTokenizer;

/**
 * Created by Carlos on 22/04/2014.
 */
public class TemperaturaService implements Runnable {

    private TemeperaturaObservable temeperaturaObservable;
    private CustomTemperaturaAdapter customTemperaturaAdapter;
    private FragmentActivity fragmentActivity;
    List<TemperaturaORM> temperaturaList;


    public TemperaturaService(CustomTemperaturaAdapter customTemperaturaAdapter, FragmentActivity fragmentActivity, List<TemperaturaORM> temperaturaList) {
        this.customTemperaturaAdapter = customTemperaturaAdapter;
        this.fragmentActivity = fragmentActivity;
        this.temperaturaList = temperaturaList;
        temeperaturaObservable = new TemeperaturaObservable();
    }


    public class TemeperaturaObservable extends Observable {
        @Override
        public void notifyObservers() {
            setChanged();
            super.notifyObservers();
        }

    }

    @Override
    public void run() {
        try {
            byte buf[] = new byte[32];
            int i = 0;
            MulticastSocket s = new MulticastSocket(Constantes.PORT_TEMPERATURA);
            s.joinGroup(InetAddress.getByName(Constantes.GROUP));
            DatagramPacket pack = new DatagramPacket(buf, buf.length);
            while (true) {
                s.receive(pack);
                byte[] data = pack.getData();
                String valueByte = new String(data);
                StringTokenizer stringTokenizer = new StringTokenizer(valueByte, ",");
                String temp = stringTokenizer.nextToken();
                TemperaturaORM temperatura = new TemperaturaORM();
                temperatura.setCreateDate(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(Calendar.getInstance().getTime()));
                temperatura.setTemp(temp);
                Log.d(Constantes.LOG_ID, this.getClass().getName() + " Valor temperatura: " + temperatura.toString());
                temperaturaList.add(temperatura);
                customTemperaturaAdapter.setDrawerItemTemperatura(temperaturaList);
                fragmentActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        temeperaturaObservable.notifyObservers();
                    }
                });
            }
        } catch (UnknownHostException e) {
            Log.e(Constantes.LOG_ID, this.getClass().getName() + e.fillInStackTrace().toString());
        } catch (IOException e) {
            Log.e(Constantes.LOG_ID, this.getClass().getName() + e.fillInStackTrace().toString());
        }
    }

    public void addObserver(Observer observer) {
        temeperaturaObservable.addObserver(observer);
    }

}
